package com.sanji.sjmallmodule.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.ImageSizeUtil;
import com.sanji.sjmallwidget.AmountView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/****
 * 配件adapter
 *****/
public class GoodsPartsAdapter extends BaseAdapter {
    private List<GoodsPartsItemBean> mData;
    private Context mContext;
    private RelativeLayout.LayoutParams imagParams = null;
    private buyGoodsPartsNumberListener listener;

    public GoodsPartsAdapter(List<GoodsPartsItemBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        //设置图片大小
        int imageWidth = ImageSizeUtil.setImageWidth(mContext, 3);
        imagParams = new RelativeLayout.LayoutParams(imageWidth, ImageSizeUtil.setImageHightSampleWidth(imageWidth));
        imagParams.setMargins(20, 20, 20, 20);
    }

    public void setOnBuyGoodsPartsNumberListener(buyGoodsPartsNumberListener listener) {
        this.listener = listener;
    }

    public void notifyAdapter(List<GoodsPartsItemBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vHolder = null;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.adapter_goods_parts_item, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        //绑定数据
        final GoodsPartsItemBean item = mData.get(i);
        vHolder.goodsPartsName.setText(item.getGoodsName());
        vHolder.goodsPartsPrice.setText(item.getPrice());
        vHolder.goodsPartsImg.setLayoutParams(imagParams);
        Glide.with(mContext)
                .load(item.getUrl())
                //使用centerCrop是利用图片图填充ImageView设置的大小，如果ImageView的Height是match_parent则图片就会被拉伸填充
                .centerCrop()
                //当加载网络图片时，由于加载过程中图片未能及时显示，此时可能需要设置等待时的图片，通过placeHolder()方法
                .placeholder(R.mipmap.default_3j_img)
                //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                .error(R.mipmap.default_3j_img)
                .crossFade()
                //DiskCacheStrategy.SOURCE：缓存原始数据，DiskCacheStrategy.RESULT：缓存变换(如缩放、裁剪等)后的资源数据，
                // DiskCacheStrategy.NONE：什么都不缓存，DiskCacheStrategy.ALL：缓存SOURC和RESULT。
                // 默认采用DiskCacheStrategy.RESULT策略，对于download only操作要使用DiskCacheStrategy.SOURCE。
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(vHolder.goodsPartsImg);
        //设置存货量
        vHolder.amountView.setGoodsStorage(10,-1,1);
        vHolder.amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                item.setGoodsStorage(amount);
                if (null != listener) {
                    listener.buyGoodsPartsNumber(item);
                }
            }
        });
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.adapter_goods_parts_img)
        ImageView goodsPartsImg;
        @BindView(R.id.adapter_goods_parts_name)
        TextView goodsPartsName;
        @BindView(R.id.adapter_goods_parts_price)
        TextView goodsPartsPrice;
        @BindView(R.id.adapter_goods_parts_amount_view)
        AmountView amountView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface buyGoodsPartsNumberListener {
        public void buyGoodsPartsNumber(GoodsPartsItemBean goodsBean);
    }
}
